package tv.dyndns.kishibe.client.setting;

import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;

import tv.dyndns.kishibe.client.Constant;
import tv.dyndns.kishibe.client.Service;
import tv.dyndns.kishibe.client.packet.PacketUserData;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.VerticalPanel;

public class PanelSettingAdministrator extends VerticalPanel implements ClickHandler {
	private final Grid grid = new Grid();
	private final Button buttonUpdate = new Button("申請リスト更新", this);

	public PanelSettingAdministrator() {
		add(new HTML("<h2>テーマモード編集権限管理</h2>"));

		grid.addStyleName("gridFrame");
		add(grid);
		add(buttonUpdate);

		updateThemeModeEditors();
	}

	private void updateThemeModeEditors() {
		Service.Util.getInstance().getThemeModeEditors(callbackGetThemeModeEditors);
	}

	private final AsyncCallback<Map<Integer, Integer>> callbackGetThemeModeEditors = new AsyncCallback<Map<Integer, Integer>>() {
		@Override
		public void onSuccess(Map<Integer, Integer> result) {
			final Map<Integer, Integer> data = new TreeMap<Integer, Integer>(result);
			grid.resize(data.size() + 1, 5);
			grid.setHTML(0, 0, "ユーザーコード");
			grid.setHTML(0, 1, "プレイヤー名");
			grid.setHTML(0, 2, "申請中");
			grid.setHTML(0, 3, "承認");
			grid.setHTML(0, 4, "却下");

			int rowCounter = 1;
			for (Entry<Integer, Integer> entry : data.entrySet()) {
				final int row = rowCounter;
				final int userCode = entry.getKey();
				final int status = entry.getValue();
				Service.Util.getInstance().loadUserData(userCode, new AsyncCallback<PacketUserData>() {
					public void onSuccess(PacketUserData result) {
						grid.setText(row, 0, "" + result.userCode);
						grid.setText(row, 1, result.playerName);
						final RadioButton buttonApplying = new RadioButton("" + userCode);
						final RadioButton buttonAccepted = new RadioButton("" + userCode);
						final RadioButton buttonRejected = new RadioButton("" + userCode);
						grid.setWidget(row, 2, buttonApplying);
						grid.setWidget(row, 3, buttonAccepted);
						grid.setWidget(row, 4, buttonRejected);

						switch (status) {
						case Constant.THEME_MODE_EDITOR_STATUS_APPLYING:
							buttonApplying.setValue(true);
							break;
						case Constant.THEME_MODE_EDITOR_STATUS_ACCEPTED:
							buttonAccepted.setValue(true);
							break;
						case Constant.THEME_MODE_EDITOR_STATUS_REJECTED:
							buttonRejected.setValue(true);
							break;
						}

						buttonApplying.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
							public void onValueChange(ValueChangeEvent<Boolean> event) {
								if (event.getValue()) {
									Service.Util.getInstance().applyThemeModeEditor(userCode, "(管理人により申請状態になりました)", Service.NULL_CALLBACK);
								}
							}
						});

						buttonAccepted.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
							public void onValueChange(ValueChangeEvent<Boolean> event) {
								if (event.getValue()) {
									Service.Util.getInstance().acceptThemeModeEditor(userCode, Service.NULL_CALLBACK);
								}
							}
						});

						buttonRejected.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
							public void onValueChange(ValueChangeEvent<Boolean> event) {
								if (event.getValue()) {
									Service.Util.getInstance().rejectThemeModeEditor(userCode, Service.NULL_CALLBACK);
								}
							}
						});
					}

					public void onFailure(Throwable caught) {
					}
				});

				++rowCounter;
			}
		}

		@Override
		public void onFailure(Throwable caught) {
			caught.printStackTrace();
		}
	};

	@Override
	public void onClick(ClickEvent event) {
		final Object source = event.getSource();
		if (source == buttonUpdate) {
			updateThemeModeEditors();
		}

	}
}
